import * as dictionary from './dictionary'

// 隐藏手机号的部分数字
exports.hidePartNum = function (val) {
  if (!val) {
    return ''
  }
  return val.replace(/^(\d{3})\d{4}(\d{4})$/, function ($0, $1, $2) {
    return $1 + '****' + $2
  })
}

// 隐藏部分姓名
exports.hidePartName = function (val) {
  if (!val) {
    return ''
  }
  let newValue = val[0]
  if (val.length === 1) {
    newValue = '*'
  }
  for (let i = 1; i < val.length; i++) {
    newValue += '*'
  }
  return newValue
}

// 隐藏身份证的部分数字
exports.hidePartIdCardNum = function (val) {
  if (!val) {
    return ''
  }
  return val.replace(/^(\d{6})\d{8}(\d{4})$/, function ($0, $1, $2) {
    return $1 + '********' + $2
  })
}

// 去除时间的秒
exports.hideSeconds = function (val) {
  if (!val) {
    return ''
  }
  var reg = /(.*):(.*):(.*)/
  val = val.replace(reg, function (match, $1, $2, $3) {
    return $1 + ':' + $2
  })
  return val
}

// 保存两位小数
exports.byteToKB = function (val) {
  return (val / 1024).toFixed(2)
}

// 去除时间
exports.hideTime = function (val) {
  if (!val) {
    return ''
  }
  var reg = /(.*)\s(.*):(.*):(.*)/
  val = val.replace(reg, function (match, $1, $2, $3, $4) {
    return $1
  })
  return val
}

// 'yyyy-MM-dd hh:mm:ss
function formatDate(dateObj, format) {
  var date = {
    'M+': dateObj.getMonth() + 1,
    'd+': dateObj.getDate(),
    'h+': dateObj.getHours(),
    'm+': dateObj.getMinutes(),
    's+': dateObj.getSeconds(),
    'q+': Math.floor((dateObj.getMonth() + 3) / 3),
    'S+': dateObj.getMilliseconds()
  }
  if (/(y+)/i.test(format)) {
    format = format.replace(RegExp.$1, (dateObj.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  for (var k in date) {
    if (new RegExp('(' + k + ')').test(format)) {
      format = format.replace(RegExp.$1, RegExp.$1.length === 1
        ? date[k] : ('00' + date[k]).substr(('' + date[k]).length))
    }
  }
  return format
}

// 时间戳转为时间 yyyy-MM-dd hh:mm:ss
exports.timestampToDate = function (value, format) {
  if (!value || value == '') {
    return ''
  }
  format = format || 'yyyy-MM-dd hh:mm:ss'
  var date = new Date()
  date.setTime(value * 1000)
  var dateStr = formatDate(date, format)
  return dateStr
}

// 转义 &lt;
exports.HTMLDecode = function (text) {
  var temp = document.createElement('div')
  temp.innerHTML = text
  var output = temp.innerText || temp.textContent
  temp = null
  return output
}

// 保留两位小数
// 保留两位小数
exports.toFixed = function (value, length) {
  if (!value) {
    return '0.00'
  }
  if (typeof value == 'string') {
    value = parseFloat(value)
  }
  return value.toFixed(length)
}

// 价格折扣 percent: 百分比 如80 代表80%
exports.discount = function (value, percent) {
  if (percent && percent != '') {
    return (value * (percent / 100)).toFixed(2)
  }
  return value
}

// 物流状态名
exports.expressStatusTxt = function (value) {
  let txt = ''
  switch (value) {
    case '0':
      txt = '在途'
      break
    case '1':
      txt = '揽件'
      break
    case '2':
      txt = '疑难'
      break
    case '3':
      txt = '签收'
      break
    case '4':
      txt = '退签'
      break
    case '5':
      txt = '派件'
      break
    case '6':
      txt = '退回'
      break
    default:
      txt = ''
  }
  return txt
}

// 快递订单状态名
exports.orderStatusTxt = function (value) {
  let txt = ''
  switch (value) {
    case dictionary.orderStatus.cancle:
      txt = '订单已取消'
      break
    case dictionary.orderStatus.unpay:
      txt = '订单未付款'
      break
    case dictionary.orderStatus.payed:
      txt = '等待卖家发货'
      break
    case dictionary.orderStatus.sended:
      txt = '卖家已发货'
      break
    case dictionary.orderStatus.recevied:
      txt = '用户已收货'
      break
  }
  return txt
}

// 自提订单状态名
exports.orderStatusTxt2 = function (value) {
  let txt = ''
  switch (value) {
    case dictionary.orderStatus.cancle:
      txt = '订单已取消'
      break
    case dictionary.orderStatus.unpay:
      txt = '订单未付款'
      break
    case dictionary.orderStatus.payed:
      txt = '等待卖家配货'
      break
    case dictionary.orderStatus.sended:
      txt = '卖家已配货'
      break
    case dictionary.orderStatus.recevied:
      txt = '用户已提货'
      break
  }
  return txt
}
exports.goodsUnitTxt = function (value) {
  let txt = ''
  switch (value) {
    case dictionary.goodsUnit.bag:
      txt = '袋装'
      break
    case dictionary.goodsUnit.pot:
      txt = '罐装'
      break
    case dictionary.goodsUnit.one:
      txt = '件'
      break
  }
  return txt
}
exports.refundStatusName = function (value) {
  let name = ''
  switch (value) {
    case dictionary.refundStatus.waiting:
      name = '等待审核'
      break
    case dictionary.refundStatus.resolve:
      name = '卖家已同意'
      break
    case dictionary.refundStatus.reject:
      name = '卖家已拒绝'
      break
  }
  return name
}

exports.userLvTypeName = function (value) {
  let name = ''
  switch (value.toString()) {
    case dictionary.userLv.a:
      name = '一级'
      break
    case dictionary.userLv.b:
      name = '二级'
      break
    case dictionary.userLv.c:
      name = '他人转入'
      break
    case dictionary.userLv.d:
      name = '团队贡献'
      break
  }
  return name
}
exports.directTypeName = function (value) {
  let name = ''
  switch (value) {
    case dictionary.directType.balance:
      name = '转入经验值'
      break
    case dictionary.directType.bankcard:
      name = '转入银行卡'
      break
  }
  return name
}
exports.withdrawStatusTxt = function (value) {
  let name = ''
  switch (value) {
    case dictionary.withdrawStatus.waiting:
      name = '审核中'
      break
    case dictionary.withdrawStatus.resolve:
      name = '审核通过'
      break
    case dictionary.withdrawStatus.reject:
      name = '审核未通过'
      break
    case dictionary.withdrawStatus.cancle:
      name = '已撤销'
      break
  }
  return name
}
// 贡献积分状态
exports.integralStatusTxt = function (value) {
  let name = ''
  switch (value) {
    case dictionary.integralStatus.normal:
      name = '正常'
      break
    case dictionary.integralStatus.freeze:
      name = '冻结'
      break
    case dictionary.integralStatus.abandon:
      name = '作废'
      break
  }
  return name
}
// 花币账单记录状态
exports.coinStatusTxt = function (value) {
  let name = ''
  switch (value) {
    case '1':
      name = '获得'
      break
    case '2':
      name = '扣除'
  }
  return name
}

// 会员状态
exports.memberActiveTxt = function (value) {
  let name = ''
  switch (value) {
    case '0':
      name = '未激活'
      break
    case '1':
      name = '激活'
  }
  return name
}
// 实名认证状态
exports.memberIdentifyTxt = function (value) {
  let name = ''
  switch (value) {
    case '0':
      name = '未认证'
      break
    case '1':
      name = '审核中'
      break
    case '2':
      name = '已认证'
      break
    case '3':
      name = '审核未通过'
      break
  }
  return name
}
